﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using PowerCreator.LMS.Common.Script;

public partial class theme_3_ChannelCommetNew : PowerCreator.LMS.Web.Base.WebBasePage
{
    protected string fileUrl = string.Empty;
    protected string url = string.Empty;
    protected string _ChannelID = "0";
    protected string _urltype = string.Empty;
    protected string _ChannelName = string.Empty;
    protected string _Author = string.Empty;
    protected string _Description = string.Empty;
    protected string _Title = string.Empty;
    protected string _contentID = string.Empty;
    protected string _uid = "0";
    protected string _ServerIP = "";


    const string SEARCH_GETSUMSCORE = "select Sum(SkillTypeScore) as SumScore from TSkillType";

    const string IMG_OK = "<img src=\"images/x1.jpg\" width=\"16\" height=\"12\" />";
    const string IMG_NO = "<img src=\"images/x2.jpg\" width=\"16\" height=\"12\" />";

    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();

    PowerCreator.LMS.ViewQuery.ViewQuery objViewQuery = new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.Web.Grade.GradeManager op = new PowerCreator.LMS.Web.Grade.GradeManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.Alert(config.GetLanagerValueByKey("theme_Channel_LoginMsg", PowerCreator.LMS.Language.LanagetItemType.Page));
            Script.CloseMe();
        }
        if (!IsPostBack)
        {
            this.Title = config.GetLanagerValueByKey("theme_ChannelComment_TitleInfo", PowerCreator.LMS.Language.LanagetItemType.Page);
            _urltype = Request.QueryString["urltype"].ToString();
            ViewState["urltype"] = _urltype;
            _contentID = Request.QueryString["ContentID"].ToString();
            ViewState["ContentID"] = _contentID;
            _ChannelID = Request.QueryString["ChannelID"].ToString();
            ViewState["ChannelID"] = _ChannelID;
            _ServerIP = Request.QueryString["ServerIP"].ToString();
            Session["ServerIP"] = _ServerIP;
            if (Request.QueryString["ChannelName"] != null)
            {
                _ChannelName = Request.QueryString["ChannelName"].ToString();
                ViewState["ChannelName"] = _ChannelName;
                _Author = Request.QueryString["Author"].ToString();
                ViewState["Author"] = _Author;
                _Description = Request.QueryString["Description"].ToString();
                ViewState["Description"] = _Description;
                _Title = Request.QueryString["Title"].ToString();
                ViewState["Title"] = _Title;

                //lab_ResourceName.Text = _ChannelName;
                //lab_Author.Text = _Author;
                //lab_channelName.Text = _ChannelName;
            }

            BinderUserLogin();
            btn_out.Text = config.GetLanagerValueByKey("SignOut", PowerCreator.LMS.Language.LanagetItemType.WebTitle);

            //初始化更新数据的URL
            // url = string.Format(URL_UPDATE_HISTORY, rid, prid, crid, courseid, projectresourceid, Base64.Encode(urid), Base64.Encode(DateTime.Now.ToString()));
            pageload();
            BinderPG(_urltype);
            _uid = GetUserID();

           
           
        }
    }

    protected void lnkbtn_EN_Click(object sender, EventArgs e)
    { 
        Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_LANGEID] = "1";
    }
    protected void lnkbtn_CN_Click(object sender, EventArgs e)
    {
        Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_LANGEID] = "0";
    }
    protected void BinderUserLogin()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            lal_UserLogin.Text = "<a  href=\"UserLogin.aspx\">" + config.GetLanagerValueByKey("SysManagerLogin", PowerCreator.LMS.Language.LanagetItemType.Page) + "</a> |  <a  href=\"Register.aspx\">" + config.GetLanagerValueByKey("SysManagerRegistor", PowerCreator.LMS.Language.LanagetItemType.Page) + "</a>";
            btn_out.Visible = false;
        }
        else
        {
            PowerCreator.LMS.Web.User.LoginEntity login = (PowerCreator.LMS.Web.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            lal_UserLogin.Text = login.AccountInfo.RealName.Trim() + "," + config.GetLanagerValueByKey("Login_msg_code5", PowerCreator.LMS.Language.LanagetItemType.Page) + "！| ";
            btn_out.Visible = true;
        }
    }
    protected void btn_out_Click(object sender, EventArgs e)
    {
        PowerCreator.LMS.Web.User.User opUser = new PowerCreator.LMS.Web.User.User();
        //opUser.UpdateOnlineUserByUserID(GetLoginEntity.UserID);
        Session.Abandon();
        PowerCreator.LMS.Common.Script.Script.ReloadMe();
    }


    private void pageload()
    {
        int sunScore = 0;
        int userNum = 0;
        DataTable dt = new DataTable();
        string sql = "select distinct FKGradeUserID from TGrade where FKGradeSkillID<>0  and FKGradeContentGuid='" + _contentID + "'";
        PowerCreator.LMS.Web.Grade.GradeManager op = new PowerCreator.LMS.Web.Grade.GradeManager();
        userNum = objViewQuery.Count(sql);
        if (userNum > 0)
        {
            sql = "select sum(GradeScore) as sum from TGrade where FKGradeContentGuid='" + _contentID + "'";
            dt = objViewQuery.Query(sql);
            if (dt.Rows.Count > 0)
            {
                sunScore = int.Parse(dt.Rows[0][0].ToString()) / userNum;
            }
            lab_AvgScore.Text = sunScore.ToString();
        }
        else
        {
            lab_AvgScore.Text = "0";
        }

        double result = Convert.ToDouble(lab_AvgScore.Text) / int.Parse(objViewQuery.Query(SEARCH_GETSUMSCORE).Rows[0][0].ToString());
        int countStart = Convert.ToInt32(result * 10);
        string str_Star = "";
        for (int i = 0; i < 10; i++)
        {

            if (i < countStart)
            {
                str_Star += IMG_OK;
            }
            else
            {
                str_Star += IMG_NO;
            }


        }
        lab_Star.Text = str_Star;
    }

    private void BinderPG(string type)
    {

        //urltype=all&ChannelID=" + dt.Rows[i]["ID"].ToString() + "&ChannelName=" + dt.Rows[i]["ChannelName"].ToString() + "&Title=" + dt.Rows[i]["Title"].ToString() + "&Author=" + dt.Rows[i]["Author"].ToString() + "&Description=" + dt.Rows[i]["Description"].ToString()
        if (type == "leftup")
        {
            fileUrl = "../../web/broadcast/frmleftup.aspx?ChannelID=" + _ChannelID + "&ChannelName=" + _ChannelName + "&Title=" + _Title + "&Author=" + _Author + "&Description=" + _Description + "&type=Vedio" + "&ServerIP=" + _ServerIP;
        }
        else if (type == "right")
        {
            fileUrl = "../../web/broadcast/frm000.aspx?ChannelID=" + _ChannelID + "&ChannelName=" + _ChannelName + "&Title=" + _Title + "&Author=" + _Author + "&Description=" + _Description + "" + "&ServerIP=" + _ServerIP;
        }
        else if (type == "all")
        {
            fileUrl = "../../web/broadcast/Content.aspx?ChannelID=" + _ChannelID + "&ChannelName=" + _ChannelName + "&Title=" + _Title + "&Author=" + _Author + "&Description=" + _Description + "" + "&ServerIP=" + _ServerIP;
        }
        else if (type == "dual")
        {
            fileUrl = "../../web/broadcast/dual/Content.aspx?ChannelID=" + _ChannelID + "&ChannelName=" + _ChannelName + "&Title=" + _Title + "&Author=" + _Author + "&Description=" + _Description + "" + "&ServerIP=" + _ServerIP;
        }

        //绑定主评论信息
        //BinderComment();
    }
    private string GetUserID()
    {
        string uid = "0";
        PowerCreator.LMS.Web.User.User opu = new PowerCreator.LMS.Web.User.User();
        DataTable dt = new DataTable();
        string sql = "StudentName='" + _Author + "'";
        dt = opu.QueryStudent(sql, 1, 1);
        if (dt.Rows.Count > 0)
        {
            uid = dt.Rows[0]["FKStudentUserId"].ToString();
        }
        else
        {
            sql = "TeacherName='" + _Author + "'";
            dt = opu.QueryTeacher(sql, 1, 1);
            if (dt.Rows.Count > 0)
            {
                uid = dt.Rows[0]["FKTeacherUserId"].ToString();
            }
        }

        return uid;

    }
    protected void btn_send_ServerClick(object sender, EventArgs e)
    {
        //PowerCreator.LMS.Grade.Entity.VOTGrade vo = new PowerCreator.LMS.Grade.Entity.VOTGrade();
        //PowerCreator.LMS.Web.Grade.GradeManager op = new PowerCreator.LMS.Web.Grade.GradeManager();
        //vo.FKGradeContentGuid = ViewState["ContentID"].ToString();
        //vo.FKGradeResourceID = 0;
        //vo.FKGradeSkillID = 0;
        ////vo.FKGradeUserID=int.Parse(Session["UserID"].ToString());
        //vo.FKGradeUserID = GetCreatedByInfo().UserID;
        ////int playtime = 0;
        ////if (ViewState["position"] !=null)
        ////    playtime = int.Parse(ViewState["position"].ToString());
        ////vo.GradeAsfTime = playtime;
        //vo.GradeContent = txt_content.Value;
        //vo.GradeCreatetime = DateTime.Now;
        //vo.GradeScore = 0;
        //op.AddGrade(vo);
        //PowerCreator.LMS.Common.Script.Script.ReloadMe();
    }

   
}
